Enables plugins to determine if the current course formatter uses sections.

From: Brandon Turner <brandont@thinkwell.com>

This patch adds course_format_uses_sections function to the /course/lib.php.
This enables plugins (or any other code) to display section information if it
makes sense for the course format.  Before this patch, activity modules
only displayed sections if the course format was "topics" or "weeks".  This
allows a more generic approach that supports new course formats without the
need to modify core code.

Course format developers should implement the callback_xxx_uses_sections method
in their lib.php file (where xxx is the name of the format).

This patch requires the get-section-name patch.
---
 course/format/topics/lang/en/format_topics.php |    4 +-
 course/format/topics/lib.php                   |   12 +++++++
 course/format/weeks/lang/en/format_weeks.php   |    2 +
 course/format/weeks/lib.php                    |   10 ++++++
 course/lib.php                                 |   16 ++++++++++
 course/resources.php                           |   18 ++++++-----
 mod/assignment/index.php                       |   33 +++++++++++---------
 mod/chat/index.php                             |   21 +++++++------
 mod/choice/index.php                           |   33 ++++++++++++--------
 mod/data/index.php                             |   21 ++++++-------
 mod/feedback/index.php                         |   26 +++++++---------
 mod/folder/index.php                           |   19 ++++++------
 mod/forum/index.php                            |   15 ++++-----
 mod/glossary/index.php                         |   35 ++++++++++++----------
 mod/hotpot/index.php                           |   24 ++++++---------
 mod/imscp/index.php                            |   19 ++++++------
 mod/lesson/index.php                           |   21 +++++++------
 mod/page/index.php                             |   19 ++++++------
 mod/resource/index.php                         |   19 ++++++------
 mod/scorm/index.php                            |   21 +++++++------
 mod/survey/index.php                           |   39 +++++++++++++-----------
 mod/url/index.php                              |   18 ++++++-----
 mod/wiki/index.php                             |   23 +++++++-------
 mod/workshop/index.php                         |   21 +++++++------
 24 files changed, 269 insertions(+), 220 deletions(-)

diff --git a/course/format/topics/lang/en/format_topics.php b/course/format/topics/lang/en/format_topics.php
index 3afe917..9f81438 100644
--- a/course/format/topics/lang/en/format_topics.php
+++ b/course/format/topics/lang/en/format_topics.php
@@ -23,6 +23,6 @@
  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
 
-$string['sectionname'] = 'topic';
+$string['sectionname'] = 'Topic';
 $string['pluginname'] = 'Topics format';
-$string['section0name'] = 'General';
\ No newline at end of file
+$string['section0name'] = 'General';
diff --git a/course/format/topics/lib.php b/course/format/topics/lib.php
index 804b0ec..8d09a4c 100644
--- a/course/format/topics/lib.php
+++ b/course/format/topics/lib.php
@@ -23,6 +23,16 @@
  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
 
+
+/**
+ * Indicates this format uses sections.
+ *
+ * @return bool Returns true
+ */
+function callback_topics_uses_sections() {
+    return true;
+}
+
 /**
  * Used to display the course structure for a course where format=topic
  *
@@ -66,4 +76,4 @@ function callback_topics_get_section_name($course, $section) {
     } else {
         return get_string('topic').' '.$section->section;
     }
-}
\ No newline at end of file
+}
diff --git a/course/format/weeks/lang/en/format_weeks.php b/course/format/weeks/lang/en/format_weeks.php
index fcdedba..55d63a5 100644
--- a/course/format/weeks/lang/en/format_weeks.php
+++ b/course/format/weeks/lang/en/format_weeks.php
@@ -23,6 +23,6 @@
  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
 
-$string['sectionname'] = 'week';
+$string['sectionname'] = 'Week';
 $string['pluginname'] = 'Weekly format';
 $string['section0name'] = 'General';
diff --git a/course/format/weeks/lib.php b/course/format/weeks/lib.php
index 61c64e2..3d01dbb 100644
--- a/course/format/weeks/lib.php
+++ b/course/format/weeks/lib.php
@@ -23,6 +23,16 @@
  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
 
+
+/**
+ * Indicates this format uses sections.
+ *
+ * @return bool Returns true
+ */
+function callback_weeks_uses_sections() {
+    return true;
+}
+
 /**
  * Used to display the course structure for a course where format=weeks
  *
diff --git a/course/lib.php b/course/lib.php
index ccf9016..1e50d76 100644
--- a/course/lib.php
+++ b/course/lib.php
@@ -3294,6 +3294,22 @@ function get_generic_section_name($format, stdClass $course, stdClass $section)
 }
 
 
+function course_format_uses_sections($format)
+{
+    global $CFG;
+
+    $featurefile = $CFG->dirroot.'/course/format/'.$format.'/lib.php';
+    $featurefunction = 'callback_'.$format.'_uses_sections';
+    if (!function_exists($featurefunction) && file_exists($featurefile)) {
+        require_once $featurefile;
+    }
+    if (function_exists($featurefunction)) {
+        return $featurefunction();
+    }
+
+    return false;
+}
+
 /**
  * Can the current user delete this course?
  * Course creators have exception,
diff --git a/course/resources.php b/course/resources.php
index db1f4b1..0c44901 100644
--- a/course/resources.php
+++ b/course/resources.php
@@ -52,8 +52,7 @@ foreach ($allmodules as $key=>$module) {
 }
 
 $strresources    = get_string('resources');
-$strweek         = get_string('week');
-$strtopic        = get_string('topic');
+$strsectionname  = get_string('sectionname', 'format_'.$course->format);
 $strname         = get_string('name');
 $strintro        = get_string('moduleintro');
 $strlastmodified = get_string('lastmodified');
@@ -65,6 +64,10 @@ $PAGE->navbar->add($strresources);
 echo $OUTPUT->header();
 
 $modinfo = get_fast_modinfo($course);
+$usesections = course_format_uses_sections($course->format);
+if ($usesections) {
+    $sections = get_all_sections($course->id);
+}
 $cms = array();
 $resources = array();
 foreach ($modinfo->cms as $cm) {
@@ -91,11 +94,8 @@ if (!$cms) {
 $table = new html_table();
 $table->attributes['class'] = 'generaltable mod_index';
 
-if ($course->format == 'weeks') {
-    $table->head  = array ($strweek, $strname, $strintro);
-    $table->align = array ('center', 'left', 'left');
-} else if ($course->format == 'topics') {
-    $table->head  = array ($strtopic, $strname, $strintro);
+if ($usesections) {
+    $table->head  = array ($strsectionname, $strname, $strintro);
     $table->align = array ('center', 'left', 'left');
 } else {
     $table->head  = array ($strlastmodified, $strname, $strintro);
@@ -108,11 +108,11 @@ foreach ($cms as $cm) {
         continue;
     }
     $resource = $resources[$cm->modname][$cm->instance];
-    if ($course->format == 'weeks' or $course->format == 'topics') {
+    if ($usesections) {
         $printsection = '';
         if ($cm->sectionnum !== $currentsection) {
             if ($cm->sectionnum) {
-                $printsection = $cm->sectionnum;
+                $printsection = get_section_name($course->format, $course, $sections[$cm->sectionnum], $sections);
             }
             if ($currentsection !== '') {
                 $table->data[] = 'hr';
diff --git a/mod/assignment/index.php b/mod/assignment/index.php
index e466863..a2e0038 100644
--- a/mod/assignment/index.php
+++ b/mod/assignment/index.php
@@ -18,8 +18,7 @@ add_to_log($course->id, "assignment", "view all", "index.php?id=$course->id", ""
 $strassignments = get_string("modulenameplural", "assignment");
 $strassignment = get_string("modulename", "assignment");
 $strassignmenttype = get_string("assignmenttype", "assignment");
-$strweek = get_string("week");
-$strtopic = get_string("topic");
+$strsectionname  = get_string('sectionname', 'format_'.$course->format);
 $strname = get_string("name");
 $strduedate = get_string("duedate", "assignment");
 $strsubmitted = get_string("submitted", "assignment");
@@ -37,15 +36,17 @@ if (!$cms = get_coursemodules_in_course('assignment', $course->id, 'cm.idnumber,
     die;
 }
 
+$usesections = course_format_uses_sections($course->format);
+if ($usesections) {
+    $sections = get_all_sections($course->id);
+}
+
 $timenow = time();
 
 $table = new html_table();
 
-if ($course->format == "weeks") {
-    $table->head  = array ($strweek, $strname, $strassignmenttype, $strduedate, $strsubmitted, $strgrade);
-    $table->align = array ("center", "left", "left", "left", "right");
-} else if ($course->format == "topics") {
-    $table->head  = array ($strtopic, $strname, $strassignmenttype, $strduedate, $strsubmitted, $strgrade);
+if ($usesections) {
+    $table->head  = array ($strsectionname, $strname, $strassignmenttype, $strduedate, $strsubmitted, $strgrade);
     $table->align = array ("center", "left", "left", "left", "right");
 } else {
     $table->head  = array ($strname, $strassignmenttype, $strduedate, $strsubmitted, $strgrade);
@@ -72,14 +73,16 @@ foreach ($modinfo->instances['assignment'] as $cm) {
     $link = "<a $class href=\"view.php?id=$cm->id\">".format_string($cm->name)."</a>";
 
     $printsection = "";
-    if ($cm->sectionnum !== $currentsection) {
-        if ($cm->sectionnum) {
-            $printsection = $cm->sectionnum;
-        }
-        if ($currentsection !== "") {
-            $table->data[] = 'hr';
+    if ($usesections) {
+        if ($cm->sectionnum !== $currentsection) {
+            if ($cm->sectionnum) {
+                $printsection = get_section_name($course->format, $course, $sections[$cm->sectionnum], $sections);
+            }
+            if ($currentsection !== "") {
+                $table->data[] = 'hr';
+            }
+            $currentsection = $cm->sectionnum;
         }
-        $currentsection = $cm->sectionnum;
     }
 
     if (!file_exists($CFG->dirroot.'/mod/assignment/type/'.$cm->assignmenttype.'/assignment.class.php')) {
@@ -111,7 +114,7 @@ foreach ($modinfo->instances['assignment'] as $cm) {
 
     $due = $cm->timedue ? userdate($cm->timedue) : '-';
 
-    if ($course->format == "weeks" or $course->format == "topics") {
+    if ($usesections) {
         $table->data[] = array ($printsection, $link, $type, $due, $submitted, $grade);
     } else {
         $table->data[] = array ($link, $type, $due, $submitted, $grade);
diff --git a/mod/chat/index.php b/mod/chat/index.php
index 2a773c1..7c31246 100644
--- a/mod/chat/index.php
+++ b/mod/chat/index.php
@@ -19,6 +19,7 @@ add_to_log($course->id, 'chat', 'view all', "index.php?id=$course->id", '');
 
 /// Get all required strings
 
+$strsectionname = get_string('sectionname', 'format_'.$course->format);
 $strchats = get_string('modulenameplural', 'chat');
 $strchat  = get_string('modulename', 'chat');
 
@@ -35,24 +36,24 @@ if (! $chats = get_all_instances_in_course('chat', $course)) {
     die();
 }
 
+$usesections = course_format_uses_sections($course->format);
+if ($usesections) {
+    $sections = get_all_sections($course->id);
+}
+
 /// Print the list of instances (your module will probably extend this)
 
 $timenow  = time();
 $strname  = get_string('name');
-$strweek  = get_string('week');
-$strtopic = get_string('topic');
 
 $table = new html_table();
 
-if ($course->format == 'weeks') {
-    $table->head  = array ($strweek, $strname);
+if ($usesections) {
+    $table->head  = array ($strsectionname, $strname);
     $table->align = array ('center', 'left');
-} else if ($course->format == 'topics') {
-    $table->head  = array ($strtopic, $strname);
-    $table->align = array ('center', 'left', 'left', 'left');
 } else {
     $table->head  = array ($strname);
-    $table->align = array ('left', 'left', 'left');
+    $table->align = array ('left');
 }
 
 $currentsection = '';
@@ -67,14 +68,14 @@ foreach ($chats as $chat) {
     $printsection = '';
     if ($chat->section !== $currentsection) {
         if ($chat->section) {
-            $printsection = $chat->section;
+            $printsection = get_section_name($course->format, $course, $sections[$chat->section], $sections);
         }
         if ($currentsection !== '') {
             $table->data[] = 'hr';
         }
         $currentsection = $chat->section;
     }
-    if ($course->format == 'weeks' or $course->format == 'topics') {
+    if ($usesection) {
         $table->data[] = array ($printsection, $link);
     } else {
         $table->data[] = array ($link);
diff --git a/mod/choice/index.php b/mod/choice/index.php
index e391759..1bc7598 100644
--- a/mod/choice/index.php
+++ b/mod/choice/index.php
@@ -18,6 +18,7 @@
 
     $strchoice = get_string("modulename", "choice");
     $strchoices = get_string("modulenameplural", "choice");
+    $strsectionname  = get_string('sectionname', 'format_'.$course->format);
     $PAGE->set_title($strchoices);
     $PAGE->navbar->add($strchoices);
     echo $OUTPUT->header();
@@ -26,6 +27,11 @@
         notice(get_string('thereareno', 'moodle', $strchoices), "../../course/view.php?id=$course->id");
     }
 
+    $usesections = course_format_uses_sections($course->format);
+    if ($usesections) {
+        $sections = get_all_sections($course->id);
+    }
+
     $sql = "SELECT cha.*
               FROM {choice} ch, {choice_answers} cha
              WHERE cha.choiceid = ch.id AND
@@ -44,11 +50,8 @@
 
     $table = new html_table();
 
-    if ($course->format == "weeks") {
-        $table->head  = array (get_string("week"), get_string("question"), get_string("answer"));
-        $table->align = array ("center", "left", "left");
-    } else if ($course->format == "topics") {
-        $table->head  = array (get_string("topic"), get_string("question"), get_string("answer"));
+    if ($usesections) {
+        $table->head  = array ($strsectionname, get_string("question"), get_string("answer"));
         $table->align = array ("center", "left", "left");
     } else {
         $table->head  = array (get_string("question"), get_string("answer"));
@@ -68,15 +71,17 @@
         } else {
             $aa = "";
         }
-        $printsection = "";
-        if ($choice->section !== $currentsection) {
-            if ($choice->section) {
-                $printsection = $choice->section;
-            }
-            if ($currentsection !== "") {
-                $table->data[] = 'hr';
+        if ($usesections) {
+            $printsection = "";
+            if ($choice->section !== $currentsection) {
+                if ($choice->section) {
+                    $printsection = get_section_name($course->format, $course, $sections[$choice->section], $sections);
+                }
+                if ($currentsection !== "") {
+                    $table->data[] = 'hr';
+                }
+                $currentsection = $choice->section;
             }
-            $currentsection = $choice->section;
         }
 
         //Calculate the href
@@ -87,7 +92,7 @@
             //Show normal if the mod is visible
             $tt_href = "<a href=\"view.php?id=$choice->coursemodule\">".format_string($choice->name,true)."</a>";
         }
-        if ($course->format == "weeks" || $course->format == "topics") {
+        if ($usesections) {
             $table->data[] = array ($printsection, $tt_href, $aa);
         } else {
             $table->data[] = array ($tt_href, $aa);
diff --git a/mod/data/index.php b/mod/data/index.php
index 0017857..baf8a4c 100755
--- a/mod/data/index.php
+++ b/mod/data/index.php
@@ -41,8 +41,7 @@ $context = get_context_instance(CONTEXT_COURSE, $course->id);
 
 add_to_log($course->id, "data", "view all", "index.php?id=$course->id", "");
 
-$strweek = get_string('week');
-$strtopic = get_string('topic');
+$strsectionname  = get_string('sectionname', 'format_'.$course->format);
 $strname = get_string('name');
 $strdata = get_string('modulename','data');
 $strdataplural  = get_string('modulenameplural','data');
@@ -56,21 +55,21 @@ if (! $datas = get_all_instances_in_course("data", $course)) {
     notice(get_string('thereareno', 'moodle',$strdataplural) , "$CFG->wwwroot/course/view.php?id=$course->id");
 }
 
+$usesections = course_format_uses_sections($course->format);
+if ($usesections) {
+    $sections = get_all_sections($course->id);
+}
+
 $timenow  = time();
 $strname  = get_string('name');
-$strweek  = get_string('week');
-$strtopic = get_string('topic');
 $strdescription = get_string("description");
 $strentries = get_string('entries', 'data');
 $strnumnotapproved = get_string('numnotapproved', 'data');
 
 $table = new html_table();
 
-if ($course->format == 'weeks') {
-    $table->head  = array ($strweek, $strname, $strdescription, $strentries, $strnumnotapproved);
-    $table->align = array ('center', 'center', 'center', 'center', 'center');
-} else if ($course->format == 'topics') {
-    $table->head  = array ($strtopic, $strname, $strdescription, $strentries, $strnumnotapproved);
+if ($usesections) {
+    $table->head  = array ($strsectionname, $strname, $strdescription, $strentries, $strnumnotapproved);
     $table->align = array ('center', 'center', 'center', 'center', 'center');
 } else {
     $table->head  = array ($strname, $strdescription, $strentries, $strnumnotapproved);
@@ -118,10 +117,10 @@ foreach ($datas as $data) {
         $rsslink = rss_get_link($context->id, $USER->id, 'data', $data->id, 'RSS');
     }
 
-    if ($course->format == 'weeks' or $course->format == 'topics') {
+    if ($usesections) {
         if ($data->section !== $currentsection) {
             if ($data->section) {
-                $printsection = $data->section;
+                $printsection = get_section_name($course->format, $course, $sections[$data->section], $sections);
             }
             if ($currentsection !== '') {
                 $table->data[] = 'hr';
diff --git a/mod/feedback/index.php b/mod/feedback/index.php
index 1e9ab62..603c7fd 100644
--- a/mod/feedback/index.php
+++ b/mod/feedback/index.php
@@ -47,30 +47,26 @@ if (! $feedbacks = get_all_instances_in_course("feedback", $course)) {
     die;
 }
 
+$usesections = course_format_uses_sections($course->format);
+if ($usesections) {
+    $sections = get_all_sections($course->id);
+}
+
 /// Print the list of instances (your module will probably extend this)
 
 $timenow = time();
 $strname  = get_string("name");
-$strweek  = get_string("week");
-$strtopic  = get_string("topic");
+$strsectionname = get_string('sectionname', 'format_'.$course->format);
 $strresponses = get_string('responses', 'feedback');
 
 $table = new html_table();
 
-if ($course->format == "weeks") {
+if ($usesections) {
     if(has_capability('mod/feedback:viewreports', $context)) {
-        $table->head  = array ($strweek, $strname, $strresponses);
+        $table->head  = array ($strsectionname, $strname, $strresponses);
         $table->align = array ("center", "left", 'center');
     }else{
-        $table->head  = array ($strweek, $strname);
-        $table->align = array ("center", "left");
-    }
-} else if ($course->format == "topics") {
-    if(has_capability('mod/feedback:viewreports', $context)) {
-        $table->head  = array ($strtopic, $strname, $strresponses);
-        $table->align = array ("center", "left", "center");
-    }else{
-        $table->head  = array ($strtopic, $strname);
+        $table->head  = array ($strsectionname, $strname);
         $table->align = array ("center", "left");
     }
 } else {
@@ -95,8 +91,8 @@ foreach ($feedbacks as $feedback) {
     $dimmedclass = $feedback->visible ? '' : 'class="dimmed"';
     $link = '<a '.$dimmedclass.' href="'.$viewurl->out().'">'.$feedback->name.'</a>';
 
-    if ($course->format == "weeks" or $course->format == "topics") {
-        $tabledata = array ($feedback->section, $link);
+    if ($usesections) {
+        $tabledata = array (get_section_name($course->format, $course, $sections[$feedback->section], $sections), $link);
     } else {
         $tabledata = array ($link);
     }
diff --git a/mod/folder/index.php b/mod/folder/index.php
index 6e5a418..3c8c9de 100644
--- a/mod/folder/index.php
+++ b/mod/folder/index.php
@@ -36,8 +36,7 @@ add_to_log($course->id, 'folder', 'view all', "index.php?id=$course->id", '');
 
 $strfolder       = get_string('modulename', 'folder');
 $strfolders      = get_string('modulenameplural', 'folder');
-$strweek         = get_string('week');
-$strtopic        = get_string('topic');
+$strsectionname  = get_string('sectionname', 'format_'.$course->format);
 $strname         = get_string('name');
 $strintro        = get_string('moduleintro');
 $strlastmodified = get_string('lastmodified');
@@ -53,14 +52,16 @@ if (!$folders = get_all_instances_in_course('folder', $course)) {
     exit;
 }
 
+$usesections = course_format_uses_sections($course->format);
+if ($usesections) {
+    $sections = get_all_sections($course->id);
+}
+
 $table = new html_table();
 $table->attributes['class'] = 'generaltable mod_index';
 
-if ($course->format == 'weeks') {
-    $table->head  = array ($strweek, $strname, $strintro);
-    $table->align = array ('center', 'left', 'left');
-} else if ($course->format == 'topics') {
-    $table->head  = array ($strtopic, $strname, $strintro);
+if ($usesections) {
+    $table->head  = array ($strsectionname, $strname, $strintro);
     $table->align = array ('center', 'left', 'left');
 } else {
     $table->head  = array ($strlastmodified, $strname, $strintro);
@@ -71,11 +72,11 @@ $modinfo = get_fast_modinfo($course);
 $currentsection = '';
 foreach ($folders as $folder) {
     $cm = $modinfo->cms[$folder->coursemodule];
-    if ($course->format == 'weeks' or $course->format == 'topics') {
+    if ($usesections) {
         $printsection = '';
         if ($folder->section !== $currentsection) {
             if ($folder->section) {
-                $printsection = $folder->section;
+                $printsection = get_section_name($course->format, $course, $sections[$folder->section], $sections);
             }
             if ($currentsection !== '') {
                 $table->data[] = 'hr';
diff --git a/mod/forum/index.php b/mod/forum/index.php
index a09e780..5016be0 100644
--- a/mod/forum/index.php
+++ b/mod/forum/index.php
@@ -67,8 +67,7 @@ $strunsubscribe  = get_string('unsubscribe', 'forum');
 $stryes          = get_string('yes');
 $strno           = get_string('no');
 $strrss          = get_string('rss');
-$strweek         = get_string('week');
-$strsection      = get_string('section');
+$strsectionname  = get_string('sectionname', 'format_'.$course->format);
 
 $searchform = forum_search_form($course);
 
@@ -103,6 +102,10 @@ if ($show_rss = (($can_subscribe || $course->id == SITEID) &&
     $generaltable->align[] = 'center';
 }
 
+$usesections = course_format_uses_sections($course->format);
+$sections = get_all_sections($course->id);
+
+$table = new html_table();
 
 // Parse and organise all the forums.  Most forums are course modules but
 // some special ones are not.  These get placed in the general forums
@@ -295,11 +298,7 @@ if ($show_rss = (($can_subscribe || $course->id == SITEID) &&
 
 if ($course->id != SITEID) {    // Only real courses have learning forums
     // Add extra field for section number, at the front
-    if ($course->format == 'weeks' or $course->format == 'weekscss') {
-        array_unshift($learningtable->head, $strweek);
-    } else {
-        array_unshift($learningtable->head, $strsection);
-    }
+    array_unshift($learningtable->head, $strsectionname);
     array_unshift($learningtable->align, 'center');
 
 
@@ -344,7 +343,7 @@ if ($course->id != SITEID) {    // Only real courses have learning forums
             $forum->intro = shorten_text(format_module_intro('forum', $forum, $cm->id), $CFG->forum_shortpost);
 
             if ($cm->sectionnum != $currentsection) {
-                $printsection = $cm->sectionnum;
+                $printsection = get_section_name($course->format, $course, $sections[$cm->sectionnum], $sections);
                 if ($currentsection) {
                     $learningtable->data[] = 'hr';
                 }
diff --git a/mod/glossary/index.php b/mod/glossary/index.php
index ce51126..afb557f 100644
--- a/mod/glossary/index.php
+++ b/mod/glossary/index.php
@@ -43,24 +43,25 @@ if (! $glossarys = get_all_instances_in_course("glossary", $course)) {
     die;
 }
 
+$usesections = course_format_uses_sections($course->format);
+if ($usesections) {
+    $sections = get_all_sections($course->id);
+}
+
 /// Print the list of instances (your module will probably extend this)
 
 $timenow = time();
+$strsectionname  = get_string('sectionname', 'format_'.$course->format);
 $strname  = get_string("name");
-$strweek  = get_string("week");
-$strtopic  = get_string("topic");
 $strentries  = get_string("entries", "glossary");
 
 $table = new html_table();
 
-if ($course->format == "weeks") {
-    $table->head  = array ($strweek, $strname, $strentries);
-    $table->align = array ("CENTER", "LEFT", "CENTER");
-} else if ($course->format == "topics") {
-    $table->head  = array ($strtopic, $strname, $strentries);
+if ($usesections) {
+    $table->head  = array ($strsectionname, $strname, $strintro);
     $table->align = array ("CENTER", "LEFT", "CENTER");
 } else {
-    $table->head  = array ($strname, $strentries);
+    $table->head  = array ($strname, $strintro);
     $table->align = array ("LEFT", "CENTER");
 }
 
@@ -84,14 +85,16 @@ foreach ($glossarys as $glossary) {
         continue;
     }
     $printsection = "";
-    if ($glossary->section !== $currentsection) {
-        if ($glossary->section) {
-            $printsection = $glossary->section;
-        }
-        if ($currentsection !== "") {
-            $table->data[] = 'hr';
+    if ($usesections) {
+        if ($glossary->section !== $currentsection) {
+            if ($glossary->section) {
+                $printsection = get_section_name($course->format, $course, $sections[$glossary->section], $sections);
+            }
+            if ($currentsection !== "") {
+                $table->data[] = 'hr';
+            }
+            $currentsection = $glossary->section;
         }
-        $currentsection = $glossary->section;
     }
 
     // TODO: count only approved if not allowed to see them
@@ -114,7 +117,7 @@ foreach ($glossarys as $glossary) {
         }
     }
 
-    if ($course->format == "weeks" or $course->format == "topics") {
+    if ($usesections) {
         $linedata = array ($printsection, $link, $count);
     } else {
         $linedata = array ($link, $count);
diff --git a/mod/hotpot/index.php b/mod/hotpot/index.php
index a44c5fd..998ecc5 100644
--- a/mod/hotpot/index.php
+++ b/mod/hotpot/index.php
@@ -27,6 +27,7 @@
     // get message strings for titles
     $strmodulenameplural = get_string("modulenameplural", "hotpot");
     $strmodulename  = get_string("modulename", "hotpot");
+    $strsectionname  = get_string('sectionname', 'format_'.$course->format);
 
     // string translation array for single and double quotes
     $quotes = array("'"=>"\'", '"'=>'&quot;');
@@ -73,6 +74,11 @@
     }
     $hotpotids = implode(',', array_keys($hotpots));
 
+    $usesections = course_format_uses_sections($course->format);
+    if ($usesections) {
+        $sections = get_all_sections($course->id);
+    }
+
     if (has_capability('mod/hotpot:grade', $sitecontext)) {
 
         // array of hotpots to be regraded
@@ -264,17 +270,7 @@
         print '<H3>'.sprintf("%0.3f", microtime_diff($start, microtime())).' secs'."</H3>\n";
     }
 
-    switch ($course->format) {
-        case 'weeks' :
-            $title = get_string("week");
-            break;
-        case 'topics' :
-            $title = get_string("topic");
-            break;
-        default :
-            $title = '';
-            break;
-    }
+    $title = $strsectionname;
     if ($title) {
         array_push($table->head, $title);
         array_push($table->align, "center");
@@ -303,8 +299,8 @@
         $printsection = "";
         if ($hotpot->section != $currentsection) {
             if ($hotpot->section) {
-                $printsection = $hotpot->section;
-                if ($course->format=='weeks' || $course->format=='topics') {
+                if ($usesections) {
+                    $printsection = get_section_name($course->format, $course, $sections[$hotpot->section], $sections);
                     // Show the zoom boxes
                     if ($displaysection==$hotpot->section) {
                         $strshowall = get_string('showall'.$course->format);
@@ -358,7 +354,7 @@
 
         $data = array ();
 
-        if ($course->format=="weeks" || $course->format=="topics") {
+        if ($usesections) {
             array_push($data, $printsection);
         }
 
diff --git a/mod/imscp/index.php b/mod/imscp/index.php
index c6e59dd..1f5444b 100644
--- a/mod/imscp/index.php
+++ b/mod/imscp/index.php
@@ -36,8 +36,7 @@ add_to_log($course->id, 'imscp', 'view all', "index.php?id=$course->id", '');
 
 $strimscp       = get_string('modulename', 'imscp');
 $strimscps      = get_string('modulenameplural', 'imscp');
-$strweek         = get_string('week');
-$strtopic        = get_string('topic');
+$strsectionname  = get_string('sectionname', 'format_'.$course->format);
 $strname         = get_string('name');
 $strintro        = get_string('moduleintro');
 $strlastmodified = get_string('lastmodified');
@@ -53,14 +52,16 @@ if (!$imscps = get_all_instances_in_course('imscp', $course)) {
     exit;
 }
 
+$usesections = course_format_uses_sections($course->format);
+if ($usesections) {
+    $sections = get_all_sections($course->id);
+}
+
 $table = new html_table();
 $table->attributes['class'] = 'generaltable mod_index';
 
-if ($course->format == 'weeks') {
-    $table->head  = array ($strweek, $strname, $strintro);
-    $table->align = array ('center', 'left', 'left');
-} else if ($course->format == 'topics') {
-    $table->head  = array ($strtopic, $strname, $strintro);
+if ($usesections) {
+    $table->head  = array ($strsectionname, $strname, $strintro);
     $table->align = array ('center', 'left', 'left');
 } else {
     $table->head  = array ($strlastmodified, $strname, $strintro);
@@ -71,11 +72,11 @@ $modinfo = get_fast_modinfo($course);
 $currentsection = '';
 foreach ($imscps as $imscp) {
     $cm = $modinfo->cms[$imscp->coursemodule];
-    if ($course->format == 'weeks' or $course->format == 'topics') {
+    if ($usesections) {
         $printsection = '';
         if ($imscp->section !== $currentsection) {
             if ($imscp->section) {
-                $printsection = $imscp->section;
+                $printsection = get_section_name($course->format, $course, $sections[$imscp->section], $sections);
             }
             if ($currentsection !== '') {
                 $table->data[] = 'hr';
diff --git a/mod/lesson/index.php b/mod/lesson/index.php
index 6f87d2b..fe9d50e 100644
--- a/mod/lesson/index.php
+++ b/mod/lesson/index.php
@@ -60,22 +60,23 @@ if (! $lessons = get_all_instances_in_course("lesson", $course)) {
     die;
 }
 
+$usesections = course_format_uses_sections($course->format);
+if ($usesections) {
+    $sections = get_all_sections($course->id);
+}
+
 /// Print the list of instances (your module will probably extend this)
 
 $timenow = time();
+$strsectionname  = get_string('sectionname', 'format_'.$course->format);
 $strname  = get_string("name");
 $strgrade  = get_string("grade");
 $strdeadline  = get_string("deadline", "lesson");
-$strweek  = get_string("week");
-$strtopic  = get_string("topic");
 $strnodeadline = get_string("nodeadline", "lesson");
 $table = new html_table();
 
-if ($course->format == "weeks") {
-    $table->head  = array ($strweek, $strname, $strgrade, $strdeadline);
-    $table->align = array ("center", "left", "center", "center");
-} else if ($course->format == "topics") {
-    $table->head  = array ($strtopic, $strname, $strgrade, $strdeadline);
+if ($usesections) {
+    $table->head  = array ($strsectionname, $strname, $strgrade, $strdeadline);
     $table->align = array ("center", "left", "center", "center");
 } else {
     $table->head  = array ($strname, $strgrade, $strdeadline);
@@ -101,7 +102,7 @@ foreach ($lessons as $lesson) {
         $due = "<font color=\"red\">".userdate($lesson->deadline)."</font>";
     }
 
-    if ($course->format == "weeks" or $course->format == "topics") {
+    if ($usesections) {
         if (has_capability('mod/lesson:manage', $context)) {
             $grade_value = $lesson->grade;
         } else {
@@ -111,10 +112,10 @@ foreach ($lessons as $lesson) {
                 $grade_value = $return[$USER->id]->rawgrade;
             }
         }
-        $table->data[] = array ($lesson->section, $link, $grade_value, $due);
+        $table->data[] = array (get_section_name($course->format, $course, $sections[$lesson->section], $sections), $link, $grade_value, $due);
     } else {
         $table->data[] = array ($link, $lesson->grade, $due);
     }
 }
 echo html_writer::table($table);
-echo $OUTPUT->footer();
\ No newline at end of file
+echo $OUTPUT->footer();
diff --git a/mod/page/index.php b/mod/page/index.php
index 391e63b..454fdb8 100644
--- a/mod/page/index.php
+++ b/mod/page/index.php
@@ -36,8 +36,7 @@ add_to_log($course->id, 'page', 'view all', "index.php?id=$course->id", '');
 
 $strpage         = get_string('modulename', 'page');
 $strpages        = get_string('modulenameplural', 'page');
-$strweek         = get_string('week');
-$strtopic        = get_string('topic');
+$strsectionname  = get_string('sectionname', 'format_'.$course->format);
 $strname         = get_string('name');
 $strintro        = get_string('moduleintro');
 $strlastmodified = get_string('lastmodified');
@@ -53,14 +52,16 @@ if (!$pages = get_all_instances_in_course('page', $course)) {
     exit;
 }
 
+$usesections = course_format_uses_sections($course->format);
+if ($usesections) {
+    $sections = get_all_sections($course->id);
+}
+
 $table = new html_table();
 $table->attributes['class'] = 'generaltable mod_index';
 
-if ($course->format == 'weeks') {
-    $table->head  = array ($strweek, $strname, $strintro);
-    $table->align = array ('center', 'left', 'left');
-} else if ($course->format == 'topics') {
-    $table->head  = array ($strtopic, $strname, $strintro);
+if ($usesections) {
+    $table->head  = array ($strsectionname, $strname, $strintro);
     $table->align = array ('center', 'left', 'left');
 } else {
     $table->head  = array ($strlastmodified, $strname, $strintro);
@@ -71,11 +72,11 @@ $modinfo = get_fast_modinfo($course);
 $currentsection = '';
 foreach ($pages as $page) {
     $cm = $modinfo->cms[$page->coursemodule];
-    if ($course->format == 'weeks' or $course->format == 'topics') {
+    if ($usesections) {
         $printsection = '';
         if ($page->section !== $currentsection) {
             if ($page->section) {
-                $printsection = $page->section;
+                $printsection = get_section_name($course->format, $course, $sections[$page->section], $sections);
             }
             if ($currentsection !== '') {
                 $table->data[] = 'hr';
diff --git a/mod/resource/index.php b/mod/resource/index.php
index fe937b9..17df839 100644
--- a/mod/resource/index.php
+++ b/mod/resource/index.php
@@ -36,8 +36,7 @@ add_to_log($course->id, 'resource', 'view all', "index.php?id=$course->id", '');
 
 $strresource     = get_string('modulename', 'resource');
 $strresources    = get_string('modulenameplural', 'resource');
-$strweek         = get_string('week');
-$strtopic        = get_string('topic');
+$strsectionname  = get_string('sectionname', 'format_'.$course->format);
 $strname         = get_string('name');
 $strintro        = get_string('moduleintro');
 $strlastmodified = get_string('lastmodified');
@@ -53,14 +52,16 @@ if (!$resources = get_all_instances_in_course('resource', $course)) {
     exit;
 }
 
+$usesections = course_format_uses_sections($course->format);
+if ($usesections) {
+    $sections = get_all_sections($course->id);
+}
+
 $table = new html_table();
 $table->attributes['class'] = 'generaltable mod_index';
 
-if ($course->format == 'weeks') {
-    $table->head  = array ($strweek, $strname, $strintro);
-    $table->align = array ('center', 'left', 'left');
-} else if ($course->format == 'topics') {
-    $table->head  = array ($strtopic, $strname, $strintro);
+if ($usesections) {
+    $table->head  = array ($strsectionname, $strname, $strintro);
     $table->align = array ('center', 'left', 'left');
 } else {
     $table->head  = array ($strlastmodified, $strname, $strintro);
@@ -71,11 +72,11 @@ $modinfo = get_fast_modinfo($course);
 $currentsection = '';
 foreach ($resources as $resource) {
     $cm = $modinfo->cms[$resource->coursemodule];
-    if ($course->format == 'weeks' or $course->format == 'topics') {
+    if ($usesections) {
         $printsection = '';
         if ($resource->section !== $currentsection) {
             if ($resource->section) {
-                $printsection = $resource->section;
+                $printsection = get_section_name($course->format, $course, $sections[$resource->section], $sections);
             }
             if ($currentsection !== '') {
                 $table->data[] = 'hr';
diff --git a/mod/scorm/index.php b/mod/scorm/index.php
index ab97cc0..be75407 100755
--- a/mod/scorm/index.php
+++ b/mod/scorm/index.php
@@ -22,8 +22,7 @@
 
     $strscorm = get_string("modulename", "scorm");
     $strscorms = get_string("modulenameplural", "scorm");
-    $strweek = get_string("week");
-    $strtopic = get_string("topic");
+    $strsectionname  = get_string('sectionname', 'format_'.$course->format);
     $strname = get_string("name");
     $strsummary = get_string("summary");
     $strreport = get_string("report",'scorm');
@@ -34,7 +33,12 @@
     $PAGE->navbar->add($strscorms);
     echo $OUTPUT->header();
 
-    if ($course->format == "weeks" or $course->format == "topics") {
+    $usesections = course_format_uses_sections($course->format);
+    if ($usesections) {
+        $sections = get_all_sections($course->id);
+    }
+
+    if ($usesections) {
         $sortorder = "cw.section ASC";
     } else {
         $sortorder = "m.timemodified DESC";
@@ -47,11 +51,8 @@
 
     $table = new html_table();
 
-    if ($course->format == "weeks") {
-        $table->head  = array ($strweek, $strname, $strsummary, $strreport);
-        $table->align = array ("center", "left", "left", "left");
-    } else if ($course->format == "topics") {
-        $table->head  = array ($strtopic, $strname, $strsummary, $strreport);
+    if ($usesections) {
+        $table->head  = array ($strsectionname, $strname, $strsummary, $strreport);
         $table->align = array ("center", "left", "left", "left");
     } else {
         $table->head  = array ($strlastmodified, $strname, $strsummary, $strreport);
@@ -62,9 +63,9 @@
 
         $context = get_context_instance(CONTEXT_MODULE,$scorm->coursemodule);
         $tt = "";
-        if ($course->format == "weeks" or $course->format == "topics") {
+        if ($usesections) {
             if ($scorm->section) {
-                $tt = "$scorm->section";
+                $tt = get_section_name($course->format, $course, $sections[$scorm->section], $sections);
             }
         } else {
             $tt = userdate($scorm->timemodified);
diff --git a/mod/survey/index.php b/mod/survey/index.php
index 55cbbe1..e55061c 100644
--- a/mod/survey/index.php
+++ b/mod/survey/index.php
@@ -17,8 +17,7 @@
     add_to_log($course->id, "survey", "view all", "index.php?id=$course->id", "");
 
     $strsurveys = get_string("modulenameplural", "survey");
-    $strweek = get_string("week");
-    $strtopic = get_string("topic");
+    $strsectionname  = get_string('sectionname', 'format_'.$course->format);
     $strname = get_string("name");
     $strstatus = get_string("status");
     $strdone  = get_string("done", "survey");
@@ -33,17 +32,19 @@
         notice(get_string('thereareno', 'moodle', $strsurveys), "../../course/view.php?id=$course->id");
     }
 
+    $usesections = course_format_uses_sections($course->format);
+    if ($usesections) {
+        $sections = get_all_sections($course->id);
+    }
+
     $table = new html_table();
 
-    if ($course->format == "weeks") {
-        $table->head  = array ($strweek, $strname, $strstatus);
-        $table->align = array ("CENTER", "LEFT", "LEFT");
-    } else if ($course->format == "topics") {
-        $table->head  = array ($strtopic, $strname, $strstatus);
-        $table->align = array ("CENTER", "LEFT", "LEFT");
+    if ($usesections) {
+        $table->head  = array ($strsectionname, $strname, $strintro);
+        $table->align = array ('CENTER', 'LEFT', 'LEFT');
     } else {
-        $table->head  = array ($strname, $strstatus);
-        $table->align = array ("LEFT", "LEFT");
+        $table->head  = array ($strname, $strintro);
+        $table->align = array ('LEFT', 'LEFT');
     }
 
     $currentsection = '';
@@ -55,14 +56,16 @@
             $ss = $strnotdone;
         }
         $printsection = "";
-        if ($survey->section !== $currentsection) {
-            if ($survey->section) {
-                $printsection = $survey->section;
-            }
-            if ($currentsection !== "") {
-                $table->data[] = 'hr';
+        if ($usesections) {
+            if ($survey->section !== $currentsection) {
+                if ($survey->section) {
+                    $printsection = get_section_name($course->format, $course, $sections[$survey->section], $sections);
+                }
+                if ($currentsection !== "") {
+                    $table->data[] = 'hr';
+                }
+                $currentsection = $survey->section;
             }
-            $currentsection = $survey->section;
         }
         //Calculate the href
         if (!$survey->visible) {
@@ -73,7 +76,7 @@
             $tt_href = "<a href=\"view.php?id=$survey->coursemodule\">".format_string($survey->name,true)."</a>";
         }
 
-        if ($course->format == "weeks" or $course->format == "topics") {
+        if ($usesections) {
             $table->data[] = array ($printsection, $tt_href, "<a href=\"view.php?id=$survey->coursemodule\">$ss</a>");
         } else {
             $table->data[] = array ($tt_href, "<a href=\"view.php?id=$survey->coursemodule\">$ss</a>");
diff --git a/mod/url/index.php b/mod/url/index.php
index ed1774e..d8bdd26 100644
--- a/mod/url/index.php
+++ b/mod/url/index.php
@@ -36,8 +36,6 @@ add_to_log($course->id, 'url', 'view all', "index.php?id=$course->id", '');
 
 $strurl       = get_string('modulename', 'url');
 $strurls      = get_string('modulenameplural', 'url');
-$strweek         = get_string('week');
-$strtopic        = get_string('topic');
 $strname         = get_string('name');
 $strintro        = get_string('moduleintro');
 $strlastmodified = get_string('lastmodified');
@@ -53,14 +51,16 @@ if (!$urls = get_all_instances_in_course('url', $course)) {
     exit;
 }
 
+$usesections = course_format_uses_sections($course->format);
+if ($usesections) {
+    $sections = get_all_sections($course->id);
+}
+
 $table = new html_table();
 $table->attributes['class'] = 'generaltable mod_index';
 
-if ($course->format == 'weeks') {
-    $table->head  = array ($strweek, $strname, $strintro);
-    $table->align = array ('center', 'left', 'left');
-} else if ($course->format == 'topics') {
-    $table->head  = array ($strtopic, $strname, $strintro);
+if ($usesections) {
+    $table->head  = array ($strsectionname, $strname, $strintro);
     $table->align = array ('center', 'left', 'left');
 } else {
     $table->head  = array ($strlastmodified, $strname, $strintro);
@@ -71,11 +71,11 @@ $modinfo = get_fast_modinfo($course);
 $currentsection = '';
 foreach ($urls as $url) {
     $cm = $modinfo->cms[$url->coursemodule];
-    if ($course->format == 'weeks' or $course->format == 'topics') {
+    if ($usesections) {
         $printsection = '';
         if ($url->section !== $currentsection) {
             if ($url->section) {
-                $printsection = $url->section;
+                $printsection = get_section_name($course->format, $course, $sections[$url->section], $sections);
             }
             if ($currentsection !== '') {
                 $table->data[] = 'hr';
diff --git a/mod/wiki/index.php b/mod/wiki/index.php
index a8653ba..09d39f0 100644
--- a/mod/wiki/index.php
+++ b/mod/wiki/index.php
@@ -64,22 +64,23 @@ if (!$wikis = get_all_instances_in_course("wiki", $course)) {
     die;
 }
 
+$usesections = course_format_uses_sections($course->format);
+if ($usesections) {
+    $sections = get_all_sections($course->id);
+}
+
 /// Print the list of instances (your module will probably extend this)
 
 $timenow = time();
+$strsectionname  = get_string('sectionname', 'format_'.$course->format);
 $strname = get_string("name");
-$strweek = get_string("week");
-$strtopic = get_string("topic");
 
-if ($course->format == "weeks") {
-    $table->head = array($strweek, $strname);
+if ($usesections) {
+    $table->head  = array ($strsectionname, $strname);
     $table->align = array("center", "left");
-} else if ($course->format == "topics") {
-    $table->head = array($strtopic, $strname);
-    $table->align = array("center", "left", "left", "left");
 } else {
-    $table->head = array($strname);
-    $table->align = array("left", "left", "left");
+    $table->head  = array ($strname);
+    $table->align = array("left");
 }
 
 foreach ($wikis as $wiki) {
@@ -91,8 +92,8 @@ foreach ($wikis as $wiki) {
         $link = "<a href=\"view.php?id=$wiki->coursemodule\">$wiki->name</a>";
     }
 
-    if ($course->format == "weeks" or $course->format == "topics") {
-        $table->data[] = array($wiki->section, $link);
+    if ($usesections) {
+        $table->data[] = array(get_section_name($course->format, $course, $sections[$wiki->section], $sections), $link);
     } else {
         $table->data[] = array($link);
     }
diff --git a/mod/workshop/index.php b/mod/workshop/index.php
index c2bd907..2ac001b 100644
--- a/mod/workshop/index.php
+++ b/mod/workshop/index.php
@@ -59,22 +59,23 @@ if (! $workshops = get_all_instances_in_course('workshop', $course)) {
     die();
 }
 
+$usesections = course_format_uses_sections($course->format);
+if ($usesections) {
+    $sections = get_all_sections($course->id);
+}
+
 /// Print the list of instances (your module will probably extend this)
 
 $timenow  = time();
+$strsectionname  = get_string('sectionname', 'format_'.$course->format);
 $strname  = get_string('name');
-$strweek  = get_string('week');
-$strtopic = get_string('topic');
 
-if ($course->format == 'weeks') {
-    $table->head  = array ($strweek, $strname);
+if ($usesections) {
+    $table->head  = array ($strsectionname, $strname);
     $table->align = array ('center', 'left');
-} else if ($course->format == 'topics') {
-    $table->head  = array ($strtopic, $strname);
-    $table->align = array ('center', 'left', 'left', 'left');
 } else {
     $table->head  = array ($strname);
-    $table->align = array ('left', 'left', 'left');
+    $table->align = array ('left');
 }
 
 foreach ($workshops as $workshop) {
@@ -86,8 +87,8 @@ foreach ($workshops as $workshop) {
         $link = "<a href=\"view.php?id=$workshop->coursemodule\">$workshop->name</a>";
     }
 
-    if ($course->format == 'weeks' or $course->format == 'topics') {
-        $table->data[] = array ($workshop->section, $link);
+    if ($usesections) {
+        $table->data[] = array (get_section_name($course->format, $course, $sections[$workshop->section], $sections), $link);
     } else {
         $table->data[] = array ($link);
     }
